//
//  playDataBase.m
//  New
//
//  Created by dlios on 15-7-24.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "playDataBase.h"

@implementation playDataBase

+(instancetype)sharPlayDataBase{

    static playDataBase *playdataBase;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playdataBase  = [[playDataBase alloc]init];
    });
    return playdataBase;
    
}
static sqlite3 *db = nil;
//打开数据库
-(void)OpenDB{
    
    if (db != nil) {
        return;
    }
    NSArray *array  =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"dataBase.sqlite"];
//    NSLog(@"***%@",sqlitePath);
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result ==SQLITE_OK) {
//                NSLog(@"打开数据库成功");
    }else
    {
        
        NSLog(@"打开数据库失败");
    }
    
}
//创建列表
-(void)creatTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS playModel(number INTEGER PRIMARY KEY AUTOINCREMENT,ID TEXT,title TEXT,contentHtml TEXT,node_color TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
        
    }
}
//创建列表
-(void)creatTable1
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS playStore(number INTEGER PRIMARY KEY AUTOINCREMENT,ID TEXT,contentHtml TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
                                NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
        
    }
}

-(void)insertNewModel:(PlayModel *)playMod{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO playModel(ID,title,contentHtml,node_color)VALUES('%@','%@','%@','%@')",playMod.ID,playMod.title,playMod.contentHtml,playMod.node_color];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                        NSLog(@"添加成功");
    }else
    {
        
        NSLog(@"添加失败");
    }
    
    
}

-(void)insertNewStoreModel:(PlayModel *)playMod{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO playStore(ID,contentHtml)VALUES('%@','%@')",playMod.ID,playMod.contentHtml];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
                                NSLog(@"添加成功");
    }else
    {
        
        NSLog(@"添加失败");
    }
    
    
}

-(NSMutableArray *)selectAllplayMod{
    
    NSString *sql = @"SELECT * FROM playModel";
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result ==SQLITE_OK) {
//                NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
            const unsigned char *title = sqlite3_column_text(stmt, 2);
            const unsigned char *contentHtml = sqlite3_column_text(stmt, 3);
            const unsigned char *node_color = sqlite3_column_text(stmt, 4);
            
            
            PlayModel *playMod = [[PlayModel alloc]init];
            playMod.ID = [NSString stringWithUTF8String:(const char *)ID];
            playMod.title = [NSString stringWithUTF8String:(const char *)title];
            playMod.contentHtml = [NSString stringWithUTF8String:(const char *)contentHtml];
            playMod.node_color = [NSString stringWithUTF8String:(const char *)node_color];
            
            [array addObject:playMod];
        }
        
        
        return array;
    }else{
        NSLog(@"准备失败");
        return array;
        
    }
    
    
}

//收藏
-(PlayModel *)selectAll:(PlayModel *)playMod{
    
    NSString *sql =[NSString stringWithFormat:@"SELECT * FROM playStore WHERE ID = '%@'",playMod.ID];
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
//    NSMutableArray *array = [NSMutableArray array];
     PlayModel *playMod1 = [[PlayModel alloc]init];
//    NSString *str = @"";
    if (result ==SQLITE_OK) {
//                        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
//            const unsigned char *title = sqlite3_column_text(stmt, 2);
//            const unsigned char *contentHtml = sqlite3_column_text(stmt, 3);
//            const unsigned char *node_color = sqlite3_column_text(stmt, 4);
            
            
           
           playMod1.ID = [NSString stringWithUTF8String:(const char *)ID];
//            playMod.title = [NSString stringWithUTF8String:(const char *)title];
//            playMod.contentHtml = [NSString stringWithUTF8String:(const char *)contentHtml];
//            playMod.node_color = [NSString stringWithUTF8String:(const char *)node_color];
            
//            [array addObject:playMod];
            
        }
        
         return playMod1;
       
    }else{
        NSLog(@"准备失败");
        return playMod1;
        
    }
    
    
}
-(NSMutableArray *)selectAllStore{
    
        NSString *sql = @"SELECT * FROM playStore";
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
        NSMutableArray *array = [NSMutableArray array];
    
    if (result ==SQLITE_OK) {
                                NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
           
            const unsigned char *contentHtml = sqlite3_column_text(stmt, 2);
            
            
            PlayModel *playMod1 = [[PlayModel alloc]init];

            playMod1.ID = [NSString stringWithUTF8String:(const char *)ID];
          
            playMod1.contentHtml = [NSString stringWithUTF8String:(const char *)contentHtml];
            [array addObject:playMod1];
            
        }
        
        return array;
        
    }else{
        NSLog(@"准备失败");
        return array;
        
    }
    
    
}


-(void)dropTable
{
    NSString *sql = @"DROP TABLE playModel";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//                        NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
    
    
}
-(void)dropTable1:(PlayModel *)playMod
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM playStore WHERE ID = '%@' and contentHtml = '%@'",playMod.ID,playMod.contentHtml];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
    NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
    
    
}
-(void)dropTable1
{
    NSString *sql = @"DROP TABLE playStore";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        //                        NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
    
    
}


@end
